package com.atguigu.ssyx.commom.result;

import lombok.Data;

/**
 * @author ZhangYK
 * @version 8.0
 * @title Result
 * @description
 * @create 2024/7/5 9:53
 */
@Data
public class Result<T> {

    //状态码
    private Integer code;
    //信息
    private String message;
    //数据
    private T data;
    //构造私有化
    private Result() {}
    //设置数据返回方法
    public static <T> Result<T> build(T data,ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<T>();
        //判断返回结果中是否需要数据
        if (data!=null){
            result.setData(data);
        }
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static <T> Result<T> build(T data,Integer code,String message) {
        Result<T> result = new Result<T>();
        //判断返回结果中是否需要数据
        if (data!=null){
            result.setData(data);
        }
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    //成功的方法
    public static<T> Result<T> ok(T data) {
        Result<T> result = build(data, ResultCodeEnum.SUCCESS);
        return result;
    }

    //失败的方法
    public static<T> Result<T> fail(T data) {
        return build(data,ResultCodeEnum.FAIL);
    }
}
